package com.liu.project.controller;

import com.liu.project.pojo.LoginUser;
import com.liu.project.pojo.User;

import com.liu.project.pojo.UserInfo;
import com.liu.project.service.UserService;
import com.liu.project.service.impl.AuthService;
import com.liu.project.utils.Message;
import com.liu.project.utils.UploadUtil;
import com.liu.project.utils.UserUtil;
import io.swagger.annotations.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static com.liu.project.utils.UploadUtil.PATH;

/**
 * @program: project
 * @description:
 * @author: 86187
 * @create: 2022-02-10 17:30
 **/

@Api(tags = "用户信息",description = "获取和修改用户的信息")
@RestController
@RequestMapping("/api/user")
public class UserApiController {

    @Autowired
    UserService userService;
    @Autowired
    AuthService authService;


     @ApiOperation(value = "获取当前登录用户账号和token值,权限")
     @GetMapping("/info")
   @ResponseBody
    public Message<LoginUser> info(){
    LoginUser loginUser = UserUtil.getLoginUser();
       loginUser.setPassword("");
        System.out.println("info:"+loginUser);
          return new Message<LoginUser>(200,"请求成功",loginUser);

     }
    @ApiOperation("获取当前登录用户详细信息")
    @GetMapping("/info/find/{id}")
    @ResponseBody
    public Message<User> findUsersById(@PathVariable("id") int id){
        User usersByUserId = userService.findUsersByUserId(id);
         String realPath= PATH + usersByUserId.getUserInfo().getImage();
         usersByUserId.getUserInfo().setImage(realPath);
        usersByUserId.setPassword("");
        return new Message<User>(200,"请求成功",usersByUserId);
    }

/*
* 添加用户的详细信息UserDetail
* */
      @ApiOperation("更新用户详细信息")
     @PostMapping("/info/update")
      @ResponseBody
    public  Message<Void> updateUsers(UserInfo userInfo){
          String username = UserUtil.getLoginUser().getUsername();
          int detailId = userService.findUserByUserName(username).getDetailId();
          userInfo.setId(detailId);
          if (userService.updateUserDetail(userInfo)){
              System.out.println("更新"+userService.updateUserDetail(userInfo));
              return new Message<>(200,"请求成功");}
          else return new Message<>(404,"请求失败");

     }

     /*修改密码*/
    @ApiOperation("修改密码")
    @PostMapping("/info/update/pwd")
    public  Message<Void> updatePwd(@RequestParam("oldPassword")String oldPassword,@RequestParam("newPassword") String newPassword,@RequestParam("checkPassword") String checkPassword){

        User loginUser = userService.findUserByUserName(UserUtil.getLoginUser().getUsername());

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (newPassword.length()<6 || newPassword.length()>10){
            return new  Message(0,"请输入6-10位密码");
        }
        if (newPassword == checkPassword){
            return new Message<>(0,"两遍密码不一致");
        }
        if (bCryptPasswordEncoder.matches(oldPassword,loginUser.getPassword())){
            return new Message<>(0,"旧密码错误");
        }

        if (userService.updatePassword(loginUser.getId(),bCryptPasswordEncoder.encode(newPassword))){
            return new Message<>(200,"请求成功");
        }
        return new Message<>(404,"请求失败");

    }


    @ApiOperation("修改头像")
   @ApiImplicitParam(name = "url" ,value="头像照片的路径")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = Message.class),
            @ApiResponse(code = 0,message = "文件夹不能为空",response = Message.class)})

    @PostMapping("/upload/header")
    @ResponseBody
    public Message<String> updateHeaderUrl(@RequestParam("url")String url) throws IOException {

        int id = UserUtil.getLoginUser().getId();
        if (StringUtils.isBlank(url)){
            return new Message(0,"文件夹不能为空");
       }

       File file = new File(url);
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

        String saveUrl="/header/"+id;
        String upload = UploadUtil.upload(multipartFile, saveUrl);
        System.out.println(upload);
        userService.updateHeader(id,upload);
         String realPath = PATH + upload;
        return new  Message<String>(200,"请求成功",realPath);
    }

}
